package kr.pe.johnkim.sendme.provider;

import java.util.ArrayList;

import kr.pe.johnkim.sendme.activity.R;
import kr.pe.johnkim.sendme.activity.SendMeDownloadMessageListActivity;
import kr.pe.johnkim.sendme.util.SendMeCommon;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class SendMeDownloadMessageAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<SendMeVoice> downloadMessageList;
	
	private SendMeVoice downloadMessage;
	LayoutInflater layoutInflater;
	
	private SendMeDownloadMessageListActivity receiveSendMeDownloadMessageListActivity;
	
	SendMeCommon sendMeCommon;
	
	String TAG = "SendMeDownloadMessageAdapter";
	
	View view;
	
	ImageButton downloadListPlayBtn;
	ImageButton menuDeleteBtn;
	
	View layoutButtons1;
	View layoutButtons2;
	
	public SendMeDownloadMessageAdapter(Context context, SendMeDownloadMessageListActivity receiveSendMeDownloadMessageListActivity, ArrayList<SendMeVoice> downloadMessageList) {
		this.context = context;
		this.downloadMessageList = downloadMessageList;
		this.receiveSendMeDownloadMessageListActivity = receiveSendMeDownloadMessageListActivity;
		
		layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		sendMeCommon = new SendMeCommon();
	}
	
	@Override
	public int getCount() {
		if(downloadMessageList!=null){
			return downloadMessageList.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return downloadMessageList.get(position);
	}


	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		view = convertView;
		
		ViewWrapper wrapper = null;
		// 태그를 이용하여 position 값을 정확한 값을 사용하도록 함. 태그를 저장후 나중에 태그값을 꺼내어 사용
		if(view == null) {
			view = layoutInflater.inflate(R.layout.activity_send_me_download_message_list_adapter, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		downloadMessage = downloadMessageList.get(position);
		
		
		downloadListPlayBtn = (ImageButton) view.findViewById(R.id.downloadListPlayBtn);
		menuDeleteBtn = (ImageButton) view.findViewById(R.id.menuDeleteBtn);					// 삭제 버튼	// 밀어서 지우기 용도
		downloadListPlayBtn.setTag(position);
		menuDeleteBtn.setTag(position);

		wrapper.getTxtViewFriendName().setTag(position);		// 혹시나
		
		layoutButtons1 = (View)view.findViewById(R.id.layout_buttons1); 
		layoutButtons2 = (View)view.findViewById(R.id.layout_buttons2); 
		
		downloadListPlayBtn.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View v) {
			
			int selectedPosition = Integer.parseInt(v.getTag()+"");
//			receiveSendMeDownloadMessageListActivity.playAudioMessage(selectedPosition);
			receiveSendMeDownloadMessageListActivity.playAudioMessage(downloadMessageList.get(selectedPosition));
//			receiveTempMessageListActivity.cancelPosition = Integer.parseInt(v.getTag()+"");
//			receiveTempMessageListActivity.confirmAndCancelUser();
			}
		});
		menuDeleteBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				int selectedPosition = Integer.parseInt(v.getTag()+"");
				receiveSendMeDownloadMessageListActivity.confirmDelete(downloadMessageList.get(selectedPosition));		 
			}
		});
//		downloadListSendBtn.setOnClickListener(new OnClickListener() {
//		@Override
//		public void onClick(View v) {
//			
//			int selectedPosition = Integer.parseInt(v.getTag()+"");
//			receiveSendMeDownloadMessageListActivity.sendAudioMessage(selectedPosition);
//			}
//		});

		// 친구 이름을 세팅 한다.
		wrapper.getTxtViewFriendName().setText(downloadMessage.getSenderName());
		
		// 친구 전화번호 세팅 한다.	  
		wrapper.getTxtViewFriendPhoneNumber().setText(sendMeCommon.phoneNumberForShowFormat(downloadMessage.getSender()));
		
		// 삭제 버튼 보일지 여부
//		if(downloadMessage.isShowDeleteBtnYn()){
		if((downloadMessageList.get(Integer.parseInt((wrapper.getTxtViewFriendName().getTag()+"")))).isShowDeleteBtnYn()){
			layoutButtons1.setVisibility(View.GONE);
			layoutButtons2.setVisibility(View.VISIBLE);
//			wrapper.getLayoutButtons1().setVisibility(View.GONE);
//			wrapper.getLayoutButtons2().setVisibility(View.VISIBLE);
		}else{
			layoutButtons1.setVisibility(View.VISIBLE);
			layoutButtons2.setVisibility(View.GONE);
//			wrapper.getLayoutButtons1().setVisibility(View.VISIBLE);
//			wrapper.getLayoutButtons2().setVisibility(View.GONE);
		}
		
		
		// 메시지 등록일 표시
		wrapper.getRegisterDate().setText(downloadMessage.getRegisterDate());
		
		return view;
	}
	
	class ViewWrapper {
		View base;
		
		TextView txtViewFriendName;				// 친구 이름.
		TextView txtViewFriendPhoneNumber;				// 친구 전화번호.
//		ImageButton imageButtonSendCheck;		// 보낼 사람 선택.
		TextView registerDate;					// 메시지 등록 일
		
//		View layoutButtons1;			// 기본 버튼 레이아웃
//		View layoutButtons2;			// 삭제 버튼 레이아웃
		
		ViewWrapper(View base) {
			this.base = base;
		}
//		
		TextView getTxtViewFriendName() {
			if(txtViewFriendName == null) {
				txtViewFriendName = (TextView)view.findViewById(R.id.txtViewFriendName);
			}
			
			return txtViewFriendName;
		}
		
		TextView getTxtViewFriendPhoneNumber() {
			if(txtViewFriendPhoneNumber == null) {
				txtViewFriendPhoneNumber = (TextView)view.findViewById(R.id.txtViewFriendPhoneNumber);
			}
			
			return txtViewFriendPhoneNumber;
		}
		
		TextView getRegisterDate() {
			if(registerDate == null) {
				registerDate = (TextView)view.findViewById(R.id.registerDate);
			}
			
			return registerDate;
		}
		
//		View getLayoutButtons1() {
//			if(layoutButtons1 == null) {
//				layoutButtons1 = (View)view.findViewById(R.id.layout_buttons1);
//			}
//			return layoutButtons1;
//		}
//		
//		View getLayoutButtons2() {
//			if(layoutButtons2 == null) {
//				layoutButtons2 = (View)view.findViewById(R.id.layout_buttons2);
//			}
//			return layoutButtons2;
//		}
		

		//		ImageButton getImageButtonSendCheck() {
//			if(imageButtonSendCheck == null) {
//				imageButtonSendCheck = (ImageButton)view.findViewById(R.id.imageButtonSendCheck);
//			}
//
//			return imageButtonSendCheck;
//		}
	}

}